﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Diagnostics;

namespace HRM.Utilities
{
    public delegate void ShowDock(string _frmName);

    public class Logger
    {
        public enum Priority : int
        {
            OFF = 200,
            DEBUG = 100,
            INFO = 75,
            WARN = 50,
            ERROR = 25,
            FATAL = 0
        }

        private static StreamWriter sw;

        private static String logDirectory;

        public static void Debug(String message)
        {
            Logger.Append(message, Priority.DEBUG);
        }

        public static void Info(String message)
        {
            Logger.Append(message, Priority.INFO);
        }

        public static void Warn(String message)
        {
            Logger.Append(message, Priority.WARN);
        }

        public static void Error(String message)
        {
            Logger.Append(message, Priority.ERROR);
        }

        public static void Fatal(String message)
        {
            Logger.Append(message, Priority.FATAL);
        }

        // constructor for static resources
        static Logger()
        {
            // load the logging path once and store in the 'logDirectory' class var
            //logDirectory ="C://SMSAuto.log";
            logDirectory = AppDomain.CurrentDomain.BaseDirectory + "SMS.log";

            // if the file doesn't exist, create it
            if (!File.Exists(logDirectory))
            {
                FileStream fs = File.Create(logDirectory);
                fs.Close();
            }
            // open up the streamwriter for writing..
            sw = File.AppendText(logDirectory);
        }

        public static void Append(String message, Priority level)
        {
            int logLevel = (int)Priority.OFF;
            // find out what logLevel we're currently at, default to 0
            String strLogLevel = "FATAL";

            switch (strLogLevel)
            {
                case "DEBUG":
                    logLevel = (int)Priority.DEBUG;
                    break;
                case "INFO":
                    logLevel = (int)Priority.INFO;
                    break;
                case "WARN":
                    logLevel = (int)Priority.WARN;
                    break;
                case "ERROR":
                    logLevel = (int)Priority.ERROR;
                    break;
                case "FATAL":
                    logLevel = (int)Priority.FATAL;
                    break;
                default:
                    logLevel = (int)Priority.OFF;
                    break;
            }

            // if this message has a priority greater than or equal to the current logging level, log it
            if (logLevel >= (int)level)
            {

                try
                {
                    lock (sw)
                    {
                        sw.Write("\r\nLog Entry : ");
                        sw.WriteLine("{0} : ", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                        sw.WriteLine("{0}", message);
                        sw.WriteLine("-------------------------------");
                        sw.Flush();
                    }
                }
                catch
                {
                    // do nothing
                }
            }
        }
    }

    public class TCParser
    {
        public static bool IsValidPhoneNumber(string _number)
        {
            if (_number.Length > 7 && _number.Length < 16)
            {
                for (int i = 0; i < _number.Length; i++)
                    if (!Char.IsDigit(_number[i]))
                        return false;
                return true;
            }
            return false;
        }

        public static bool IsValidEmail(string _email)
        {
            string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
                                  + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                                  + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                                  + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                                  + @"[a-zA-Z]{2,}))$";

            Regex reStrict = new Regex(patternStrict);

            return reStrict.IsMatch(_email);
        }
    }

    public class CharsetUtil
    {
        public static string UnicodeMap = "áàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ";
        public static string[] VIQRMap = new string[] { "a'", "a`", "a?", "a~", "a.", "a(", "a('", "a(`", "a(?", "a(~", "a(.", "a^", "a^'", "a^`", "a^?", "a^~", "a^.", "e'", "e`", "e?", "e~", "e.", "e^", "e^'", "e^`", "e^?", "e^~", "e^.", "i'", "i`", "i?", "i~", "i.", "o'", "o`", "o?", "o~", "o.", "o^", "o^'", "o^`", "o^?", "o^~", "o^.", "o+", "o+'", "o+`", "o+?", "o+~", "o+.", "u'", "u`", "u?", "u~", "u.", "u+", "u+'", "u+`", "u+?", "u+~", "u+.", "y'", "y`", "y?", "y~", "y.", "dd", "A'", "A`", "A?", "A~", "A.", "A(", "A('", "A(`", "A(?", "A(~", "A(.", "A^", "A^'", "A^`", "A^?", "A^~", "A^.", "E'", "E`", "E?", "E~", "E.", "E^", "E^'", "E^`", "E^?", "E^~", "E^.", "I'", "I`", "I?", "I~", "I.", "O'", "O`", "O?", "O~", "O.", "O^", "O^'", "O^`", "O^?", "O^~", "O^.", "O+", "O+'", "O+`", "O+?", "O+~", "O+.", "U'", "U`", "U?", "U~", "U.", "U+", "U+'", "U+`", "U+?", "U+~", "U+.", "Y'", "Y`", "Y?", "Y~", "Y.", "DD" };
        public static string[] VIQRMapReg = new string[] {	@"a'", "á",
															 @"a`", "à",
															 @"a\?", "ả",
															 @"a~", "ã",
															 @"a\.", "ạ",															 
															 @"a\('", "ắ",
															 @"a\(`", "ằ",
															 @"a\(\?", "ẳ",
															 @"a\(~", "ẵ",
															 @"a\(\.", "ặ",
															 @"a\(", "ă",															 
															 @"a\^'", "ấ",
															 @"a\^`", "ầ",
															 @"a\^\?", "ẩ",
															 @"a\^~", "ẫ",
															 @"a\^\.", "ậ",
															 @"a\^", "â",
															 @"e'", "é",
															 @"e`", "è",
															 @"e\?", "ẻ",
															 @"e~", "ẽ",
															 @"e\.", "ẹ",															 
															 @"e\^'", "ế",
															 @"e\^`", "ề",
															 @"e\^\?", "ể",
															 @"e\^~", "ễ",
															 @"e\^\.", "ệ",
															 @"e\^", "ê",
															 @"i'", "í",
															 @"i`", "ì",
															 @"i\?", "ỉ",
															 @"i~", "ĩ",
															 @"i\.", "ị",
															 @"o'", "ó",
															 @"o`", "ò",
															 @"o\?", "ỏ",
															 @"o~", "õ",
															 @"o\.", "ọ",															 
															 @"o\^'", "ố",
															 @"o\^`", "ồ",
															 @"o\^\?", "ổ",
															 @"o\^~", "ỗ",
															 @"o\^\.", "ộ",
															 @"o\^", "ô",															 
															 @"o\+'", "ớ",
															 @"o\+`", "ờ",
															 @"o\+\?", "ở",
															 @"o\+~", "ỡ",
															 @"o\+\.", "ợ",
															 @"o\+", "ơ",
															 @"u'", "ú",
															 @"u`", "ù",
															 @"u\?", "ủ",
															 @"u~", "ũ",
															 @"u\.", "ụ",															 
															 @"u\+'", "ứ",
															 @"u\+`", "ừ",
															 @"u\+\?", "ử",
															 @"u\+~", "ữ",
															 @"u\+\.", "ự",
															 @"u\+", "ư",
															 @"y'", "ý",
															 @"y`", "ỳ",
															 @"y\?", "ỷ",
															 @"y~", "ỹ",
															 @"y\.", "ỵ",
															 @"dd", "đ",
															 @"A'", "Á",
															 @"A`", "À",
															 @"A\?", "Ả",
															 @"A~", "Ã",
															 @"A\.", "Ạ",															 
															 @"A\('", "Ắ",
															 @"A\(`", "Ằ",
															 @"A\(\?", "Ẳ",
															 @"A\(~", "Ẵ",
															 @"A\(\.", "Ặ",
															 @"A\(", "Ă",															 
															 @"A\^'", "Ấ",
															 @"A\^`", "Ầ",
															 @"A\^\?", "Ẩ",
															 @"A\^~", "Ẫ",
															 @"A\^\.", "Ậ",
															 @"A\^", "Â",
															 @"E'", "É",
															 @"E`", "È",
															 @"E\?", "Ẻ",
															 @"E~", "Ẽ",
															 @"E\.", "Ẹ",															 
															 @"E\^'", "Ế",
															 @"E\^`", "Ề",
															 @"E\^\?", "Ể",
															 @"E\^~", "Ễ",
															 @"E\^\.", "Ệ",
															 @"E\^", "Ê",
															 @"I'", "Í",
															 @"I`", "Ì",
															 @"I\?", "Ỉ",
															 @"I~", "Ĩ",
															 @"I\.", "Ị",
															 @"O'", "Ó",
															 @"O`", "Ò",
															 @"O\?", "Ỏ",
															 @"O~", "Õ",
															 @"O\.", "Ọ",															 
															 @"O\^'", "Ố",
															 @"O\^`", "Ồ",
															 @"O\^\?", "Ổ",
															 @"O\^~", "Ỗ",
															 @"O\^\.", "Ộ",
															 @"O\^", "Ô",															 
															 @"O\+'", "Ớ",
															 @"O\+`", "Ờ",
															 @"O\+\?", "Ở",
															 @"O\+~", "Ỡ",
															 @"O\+\.", "Ợ",
															 @"O\+", "Ơ",
															 @"U'", "Ú",
															 @"U`", "Ù",
															 @"U\?", "Ủ",
															 @"U~", "Ũ",
															 @"U\.", "Ụ",															 
															 @"U\+'", "Ứ",
															 @"U\+`", "Ừ",
															 @"U\+\?", "Ử",
															 @"U\+~", "Ữ",
															 @"U\+\.", "Ự",
															 @"U\+", "Ư",
															 @"Y'", "Ý",
															 @"Y`", "Ỳ",
															 @"Y\?", "Ỷ",
															 @"Y~", "Ỹ",
															 @"Y\.", "Ỵ",
															 @"DD", "Đ"};
        public static string TCVN3Map = "¸µ¶·¹¨¾»¼½Æ©ÊÇÈÉËÐÌÎÏÑªÕÒÓÔÖÝ×ØÜÞãßáâä«èåæçé¬íêëìîóïñòô­øõö÷ùýúûüþ®¸µ¶·¹¡¾»¼½Æ¢ÊÇÈÉËÐÌÎÏÑ£ÕÒÓÔÖÝ×ØÜÞãßáâä¤èåæçé¥íêëìîóïñòô¦øõö÷ùýúûüþ§";

        public static string Convert(string InputString, string DestinationCharMap)
        {
            return Convert(InputString, "UNICODE", DestinationCharMap);
        }

        public static string ConvertToVIQR(string InputString)
        {
            return Convert(InputString, "UNICODE", "VIQR");
        }

        public static string Convert(string InputString, string SourceCharMap, string DestinationCharMap)
        {
            string retVal = "";
            StringBuilder sb = new StringBuilder();
            int Pos = 0;
            int l = InputString.Length;
            int i = 0;
            Regex r;
            Match m;
            if (SourceCharMap != "VIQR")
            {
                for (i = 0; i < l; i++)
                {
                    Pos = -1;
                    switch (SourceCharMap)
                    {
                        case "UNICODE":
                            Pos = UnicodeMap.IndexOf(InputString[i].ToString());
                            break;
                        case "TCVN3":
                            r = new Regex(InputString[i].ToString());
                            m = r.Match(TCVN3Map);
                            if (m.Success)
                            {
                                Pos = m.Index;
                            }
                            break;
                    }
                    if (Pos < 0)
                    {
                        sb.Append(InputString[i]);
                    }
                    else
                    {
                        switch (DestinationCharMap)
                        {
                            case "UNICODE":
                                sb.Append(UnicodeMap[Pos]);
                                break;
                            case "VIQR":
                                sb.Append(VIQRMap[Pos]);
                                break;
                            case "TCVN3":
                                sb.Append(TCVN3Map[Pos]);
                                break;
                        }
                    }
                }
                retVal = sb.ToString();
            }
            else
            {
                retVal = InputString;
                l = VIQRMapReg.Length;
                for (i = 0; i < l; i = i + 2)
                {
                    r = new Regex(VIQRMapReg[i]);
                    retVal = r.Replace(retVal, VIQRMapReg[i + 1]);
                }
                if (DestinationCharMap != "UNICODE")
                    retVal = Convert(retVal, "UNICODE", DestinationCharMap);
            }
            return retVal;
        }
    }

    public class UnicodeConvert
    {
        private static char[] tcvnchars = {
        'µ', '¸', '¶', '·', '¹', 
        '¨', '»', '¾', '¼', '½', 'Æ', 
        '©', 'Ç', 'Ê', 'È', 'É', 'Ë', 
        '®', 'Ì', 'Ð', 'Î', 'Ï', 'Ñ', 
        'ª', 'Ò', 'Õ', 'Ó', 'Ô', 'Ö', 
        '×', 'Ý', 'Ø', 'Ü', 'Þ', 
        'ß', 'ã', 'á', 'â', 'ä', 
        '«', 'å', 'è', 'æ', 'ç', 'é', 
        '¬', 'ê', 'í', 'ë', 'ì', 'î', 
        'ï', 'ó', 'ñ', 'ò', 'ô', 
        '­', 'õ', 'ø', 'ö', '÷', 'ù', 
        'ú', 'ý', 'û', 'ü', 'þ', 
        '¡', '¢', '§', '£', '¤', '¥', '¦'
    };

        private static char[] unichars = {
        'à', 'á', 'ả', 'ã', 'ạ', 
        'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ', 
        'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 
        'đ', 'è', 'é', 'ẻ', 'ẽ', 'ẹ', 
        'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ', 
        'ì', 'í', 'ỉ', 'ĩ', 'ị', 
        'ò', 'ó', 'ỏ', 'õ', 'ọ', 
        'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 
        'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ', 
        'ù', 'ú', 'ủ', 'ũ', 'ụ', 
        'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự', 
        'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ', 
        'Ă', 'Â', 'Đ', 'Ê', 'Ô', 'Ơ', 'Ư'
    };

        private static char[] tcvncharsA = {
        'µ', '¸', '¶', '·', '¹', 
        '»', '¾', '¼', '½', 'Æ', 
        'Ç', 'Ê', 'È', 'É', 'Ë', 
        'Ì', 'Ð', 'Î', 'Ï', 'Ñ', 
        'Ò', 'Õ', 'Ó', 'Ô', 'Ö', 
        '×', 'Ý', 'Ø', 'Ü', 'Þ', 
        'ß', 'ã', 'á', 'â', 'ä', 
        'å', 'è', 'æ', 'ç', 'é', 
        'ê', 'í', 'ë', 'ì', 'î', 
        'ï', 'ó', 'ñ', 'ò', 'ô', 
        'õ', 'ø', 'ö', '÷', 'ù', 
        'ú', 'ý', 'û', 'ü', 'þ', 
        '¡', '¢', '§', '£', '¤', '¥', '¦',
        'µ', '¸', '¶', '·', '¹', 
        '¨', '»', '¾', '¼', '½', 'Æ', 
        '©', 'Ç', 'Ê', 'È', 'É', 'Ë', 
        '®', 'Ì', 'Ð', 'Î', 'Ï', 'Ñ', 
        'ª', 'Ò', 'Õ', 'Ó', 'Ô', 'Ö', 
        '×', 'Ý', 'Ø', 'Ü', 'Þ', 
        'ß', 'ã', 'á', 'â', 'ä', 
        '«', 'å', 'è', 'æ', 'ç', 'é', 
        '¬', 'ê', 'í', 'ë', 'ì', 'î', 
        'ï', 'ó', 'ñ', 'ò', 'ô', 
        '­', 'õ', 'ø', 'ö', '÷', 'ù', 
        'ú', 'ý', 'û', 'ü', 'þ'
    };

        private static char[] unicharsA = {
        'À', 'Á', 'Ả', 'Ã', 'Ạ', 
        'Ằ', 'Ắ', 'Ẳ', 'Ẵ', 'Ặ', 
        'Ầ', 'Ấ', 'Ẩ', 'Ẫ', 'Ậ', 
        'È', 'É', 'Ẻ', 'Ẽ', 'Ẹ', 
        'Ề', 'Ế', 'Ể', 'Ễ', 'Ệ', 
        'Ì', 'Í', 'Ỉ', 'Ĩ', 'Ị', 
        'Ò', 'Ó', 'Ỏ', 'Õ', 'Ọ', 
        'Ồ', 'Ố', 'Ổ', 'Ỗ', 'Ộ', 
        'Ờ', 'Ớ', 'Ở', 'Ỡ', 'Ợ', 
        'Ù', 'Ú', 'Ủ', 'Ũ', 'Ụ', 
        'Ừ', 'Ứ', 'Ử', 'Ữ', 'Ự', 
        'Ỳ', 'Ý', 'Ỷ', 'Ỹ', 'Ỵ', 
        'Ă', 'Â', 'Đ', 'Ê', 'Ô', 'Ơ', 'Ư',
        'à', 'á', 'ả', 'ã', 'ạ', 
        'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ', 
        'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 
        'đ', 'è', 'é', 'ẻ', 'ẽ', 'ẹ', 
        'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ', 
        'ì', 'í', 'ỉ', 'ĩ', 'ị', 
        'ò', 'ó', 'ỏ', 'õ', 'ọ', 
        'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 
        'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ', 
        'ù', 'ú', 'ủ', 'ũ', 'ụ', 
        'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự', 
        'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ'
    };


        private static char[] convertTable;

        static UnicodeConvert()
        {
            convertTable = new char[256];

            for (int i = 0; i < 256; i++)
                convertTable[i] = (char)i;

            for (int i = 0; i < tcvnchars.Length; i++)
                convertTable[tcvnchars[i]] = unichars[i];

        }

        public static string UnicodeToTCVN3(string value)
        {
            for (int i = 0; i < unicharsA.Length; i++)
            {
                value = value.Replace(unicharsA[i], tcvncharsA[i]);
            }
            return value;
        }

        /// <summary>
        /// Convert chuỗi từ định dạng font TCVN3 sang Unicode
        /// Có kiểm tra đã là Unicode rồi thì không convert nữa
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string TCVN3ToUnicode(string value)
        {
            //Nếu không phải là định dạng Unicode thì mới convert ngược lại
            if (!IsUnicodeFormat(value))
            {
                char[] chars = value.ToCharArray();
                for (int i = 0; i < chars.Length; i++)
                    if (chars[i] < (char)256)
                        chars[i] = convertTable[chars[i]];
                return new string(chars);
            }
            else
                return value;
        }

        public static bool IsUnicodeFormat(string value)
        {
            value = value.ToLower();
            bool retVal =true;
            char []charContent = value.ToCharArray();
            foreach (char c in charContent)
            {
                if (Array.IndexOf(unichars, c) < 0)
                {
                    retVal = false;
                    break;
                }
            }
            return retVal;

            //byte[] abCharacters = System.Text.Encoding.ASCII.GetBytes(value);
            //string sConverted = System.Text.Encoding.ASCII.GetString(abCharacters);
            //return (value == sConverted);
        }

        public static void UnicodeToTCVN3(ref DataTable dt)
        {
            foreach (DataColumn col in dt.Columns)
            {
                col.ColumnName = UnicodeToTCVN3(col.ColumnName);
            }
            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataColumn dCol in dt.Columns)
                {
                    if (dCol.DataType == typeof(String))
                    {
                        dr[dCol] = UnicodeToTCVN3(Convert.ToString(dr[dCol]));
                    }
                }
            }
        }
    }

    public class DateTimeUtil
    {
        public static int MonthDiff(DateTime FromDate, DateTime ToDate)
        {
            return (ToDate.Year - FromDate.Year) * 12 + (ToDate.Month - FromDate.Month);
        }
    }

    public class MSWordUtil
    {
        //CallBackk
        public delegate void CallbackFill(XmlNode dataNode, string field, object data);
        public static event CallbackFill CallBack;

        public static Stream GetEmbeddedFile(string fileName)
        {
            try
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.Load("IBMSTemplate");
                Stream str = a.GetManifestResourceStream("IBMSTemplate.Templates." + fileName);
                if (str == null)
                    throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly ");
                return str;
            }
            catch (Exception e)
            {
                throw new Exception("IBMS2008 : " + e.Message);
            }
        }

        public static void TemplateDoc(string _fileName, DataSet _dsData)
        {
            XmlDocument xmlTemplateDoc = new XmlDocument();
            //xmlTemplateDoc.Load(_fileName);
            xmlTemplateDoc.Load(GetEmbeddedFile(_fileName));
            CExWordMLFiller filler = new CExWordMLFiller(_dsData, xmlTemplateDoc.OuterXml);
            if (!filler.OperationFailed)
            {
                filler.Transform();
                if (filler.OperationFailed)
                {
                    foreach (string err in filler.ErrorList)
                    {
                        MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }
                string copyFileName = Path.GetTempFileName() + ".xml";
                filler.WordMLDocument.Save(copyFileName);

                Process.Start(copyFileName);
            }
            else
            {
                foreach (string err in filler.ErrorList)
                {
                    MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public static string CreateTemplateDoc(string _fileName, DataSet _dsData)
        {
            XmlDocument xmlTemplateDoc = new XmlDocument();
            //xmlTemplateDoc.Load(_fileName);
            if (_fileName.Trim().Length == 0)
            {
                return "";
            }
            try
            {
                xmlTemplateDoc.Load(_fileName);
                //xmlTemplateDoc.Load(GetEmbeddedFile(_fileName));

                CExWordMLFiller filler = new CExWordMLFiller(_dsData, xmlTemplateDoc.OuterXml);
                CExWordMLFiller.CallBack += new CExWordMLFiller.CallbackFill((XmlNode node, string field, object data) => {
                    if(CallBack!=null)
                        CallBack(node, field, data);
                });

                if (!filler.OperationFailed)
                {
                    filler.Transform();
                    if (filler.OperationFailed)
                    {
                        foreach (string err in filler.ErrorList)
                        {
                            MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        return "";
                    }
                    string copyFileName = Path.GetTempFileName() + ".doc";
                    filler.WordMLDocument.Save(copyFileName);

                    return copyFileName;
                }
                else
                {
                    foreach (string err in filler.ErrorList)
                    {
                        MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }
        }

        public static string CreateTemplateDoc(string _fileName, DataSet _dsData, string imagePath)
        {
            XmlDocument xmlTemplateDoc = new XmlDocument();
            if (_fileName.Trim().Length == 0)
            {
                return "";
            }
            try
            {
                xmlTemplateDoc.Load(GetEmbeddedFile(_fileName));
                CExWordMLFiller filler = new CExWordMLFiller(_dsData, xmlTemplateDoc.OuterXml);
                if (!filler.OperationFailed)
                {
                    filler.Transform(imagePath);
                    if (filler.OperationFailed)
                    {
                        foreach (string err in filler.ErrorList)
                        {
                            MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        return "";
                    }
                    string copyFileName = Path.GetTempFileName() + ".doc";
                    //copyFileNameSections[0].Tables.RemoveAt(0);
                    filler.WordMLDocument.Save(copyFileName);


                    return copyFileName;
                }
                else
                {
                    foreach (string err in filler.ErrorList)
                    {
                        MessageBox.Show(err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }
        }
    }

    public static class Util
    {
        //Tạo folder trên desktop để lưu
        public static string CreateFolderTemplate()
        {
            string sPath = "";
            try
            {
                sPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HRM_BHXH_ONLINE");
                if (!Directory.Exists(sPath))
                    Directory.CreateDirectory(sPath);
            }
            catch (Exception ex)
            {

            }
            return sPath;
        }
    }
}


